package com.ks.cms;


import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kingdee
 * @ClassName: swagger2配置
 * @Description:
 * @date 2019.11.28
 */

@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {

    /*//使用项目的配置以及开关Swagger
    @Value("${app.name}")
    private String appName;
    @Value("${app.discription}")
    private String appDiscription;
    @Value("${app.company}")
    private String Company;
    @Value("${app.email}")
    private String Email;
    @Value("${app.homepage}")
    private String homePage;
    @Value("${app.apiVersion}")
    private String apiVersion;
    @Value("${app.swagger-enable}")
    private boolean enableSwagger;
    @Value("${app.token-enable}")
    private boolean tokenEnable;*/

    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ks.cms.controller"))
                .paths(PathSelectors.any())
                /* .paths(Predicates.not(PathSelectors.regex("/error.*")))// 错误路径不监控
                 .paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控*/
                .build()
                .globalOperationParameters(globalOperation())
                .apiInfo(apiInfo());
    }

  /*  private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(appName)
                .description(appDiscription)
                .contact(new Contact(Company, homePage, Email))
                .version(apiVersion)
                .build();
    }*/
  private ApiInfo apiInfo() {
      return new ApiInfoBuilder()
              .title("信息发布")
              .description("信息发布")
              .contact(new Contact("信息发布service-cms", "", "hedong@163.com"))
              .version("1.0")
              .build();
  }

/*    private List<Parameter> globalOperation(){
        //添加head参数配置start
        //第一个token为传参的key，第二个token为swagger页面显示的值
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("AuthorToken")
                .description("Api访问令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(tokenEnable).build();
        pars.add(tokenPar.build());
        return pars;
    }*/
    private List<Parameter> globalOperation(){
        //添加head参数配置start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        //第一个token为传参的key，第二个token为swagger页面显示的值
        tokenPar.name("AuthorToken").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return pars;
    }

}